package com.yz.learn.service.impl;

import com.github.pagehelper.PageHelper;
import com.yz.learn.entity.SysRole;
import com.yz.learn.exception.BusinessException;
import com.yz.learn.exception.code.BaseResponseCode;
import com.yz.learn.mapper.SysRoleMapper;
import com.yz.learn.service.PermissionService;
import com.yz.learn.service.RolePermissionService;
import com.yz.learn.service.RoleService;
import com.yz.learn.utils.PageUtil;
import com.yz.learn.vo.req.RoleAddReqVO;
import com.yz.learn.vo.req.RolePageReqVO;
import com.yz.learn.vo.req.RolePermissionOperationReqVO;
import com.yz.learn.vo.resp.PageVO;
import com.yz.learn.vo.resp.PermissionRespNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private SysRoleMapper sysRoleMapper;


    @Autowired
    private RolePermissionService rolePermissionService;



    @Override
    @Cacheable(value = "aaa")
    public PageVO<SysRole> pageInfo(RolePageReqVO vo) {
        PageHelper.startPage(vo.getPageNum() , vo.getPageSize());
        List<SysRole> sysRoleList = selectAll(vo);
        return PageUtil.getPageVO(sysRoleList);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysRole addRole(RoleAddReqVO vo) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(vo,sysRole);
        sysRole.setId(UUID.randomUUID().toString());
        sysRole.setCreateTime(new Date());
        int count = sysRoleMapper.insertSelective(sysRole);
        if(count==0)
            throw new BusinessException(BaseResponseCode.OPERATION_EROR);
        RolePermissionOperationReqVO reqVo = new RolePermissionOperationReqVO();
        reqVo.setRoleId(sysRole.getId());
        reqVo.setPermissionIds(vo.getPermissionIds());
        rolePermissionService.addRolePermission(reqVo);
        return sysRole;
    }

    @Override
    public List<SysRole> selectAll(RolePageReqVO vo) {
        return  sysRoleMapper.selectAll(vo);
    }

    @Override
    public SysRole detailInfo(String id) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
        if(sysRole==null)
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        List<PermissionRespNode> permissionRespNodes = permissionService.selectAllByTree();
        Set<String> checkList = new HashSet<>(rolePermissionService.getPermissionIdsByRoleId(id));
        setChecked(permissionRespNodes, checkList);
        sysRole.setPermissionRespNodes(permissionRespNodes);
        return sysRole;
    }

    private void setChecked(List<PermissionRespNode> permissionRespNodes, Set<String> checkList) {
        for(PermissionRespNode permissionRespNode: permissionRespNodes){
            /**
             * 子集选中从它往上到跟目录都被选中，父级选中从它到它所有的叶子节点都会被选中
             * 这样我们就直接遍历最底层及就可以了
             */
            if(checkList.contains(permissionRespNode)
                    && (permissionRespNode.getChildren()==null || permissionRespNode.getChildren().isEmpty())){
                permissionRespNode.setChecked(true);
            }
            setChecked((List<PermissionRespNode>)permissionRespNode.getChildren(),checkList);
        }

    }


}
